All files / src/services reseller.ts

0% Statements 0/56
100% Branches 0/0
0% Functions 0/13
0% Lines 0/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
import { apiService } from './api';
import { ApiResult } from '@/types';
import { User, Device, CreateDemoRequest, DemoStats } from '@/types';
import { UpdateUserRequest } from './user';
import { API_ENDPOINTS } from '@/constants/api';
 
export interface CreditBalance {
  credits: number;
}
 
export interface CreditCalculation {
  devices: number;
  credit_cost: number;
  current_credits: number;
  sufficient_credits: boolean;
}
 
export interface CreditRates {
  default_devices: number;
  credit_per_user_3_devices: number;
  credit_per_user_5_devices: number;
  credit_per_extra_device: number;
}
 
export interface CreateResellerEndUserRequest {
  username: string;
  password: string;
  email: string;
  max_devices: number;
  expires_at?: string;
  category_ids?: number[];
}
 
export interface UserDevicesResponse {
  user_id: number;
  devices: Device[];
  device_count: number;
}
 
class ResellerService {
 
 
  /**
   * Get credit rates configuration
   */
  async getCreditRates(): Promise<ApiResult<CreditRates>> {
    try {
      const result = await apiService.get<CreditRates>(API_ENDPOINTS.RESELLER.CREDIT_RATES);
      return result;
    } catch {
      return {
        success: false,
        error: {
          error: 'Credit Rates Fetch Failed',
          details: 'Failed to fetch credit rates',
          timestamp: new Date().toISOString()}
      };
    }
  }
 
  /**
   * Get current credit balance
   */
  async getCreditBalance(): Promise<ApiResult<{ credits: number }>> {
    try {
      const result = await apiService.get<{ credits: number }>(`${API_ENDPOINTS.RESELLER.CREDITS}`);
      return result;
    } catch {
      return {
        success: false,
        error: {
          error: 'Credit Balance Fetch Failed',
          details: 'Failed to fetch current credit balance',
          timestamp: new Date().toISOString()}
      };
    }
  }
 
  /**
   * Calculate credit cost for creating end user
   */
  async calculateCreditCost(devices: number): Promise<ApiResult<CreditCalculation>> {
    try {
      const result = await apiService.get<CreditCalculation>(
        API_ENDPOINTS.RESELLER.CALCULATE_CREDITS(devices)
      );
      return result;
    } catch {
      return {
        success: false,
        error: {
          error: 'Credit Calculation Failed',
          details: 'Failed to calculate credit cost',
          timestamp: new Date().toISOString()}
      };
    }
  }
 
  /**
   * Get reseller's end users
   */
  async getEndUsers(): Promise<ApiResult<User[]>> {
    try {
      const result = await apiService.get<User[]>(API_ENDPOINTS.RESELLER.USERS);
      return result;
    } catch {
      return {
        success: false,
        error: {
          error: 'End Users Fetch Failed',
          details: 'Failed to fetch end users',
          timestamp: new Date().toISOString()}
      };
    }
  }
 
  /**
   * Create new end user
   */
  async createEndUser(userData: CreateResellerEndUserRequest): Promise<ApiResult<User>> {
    try {
      const result = await apiService.post<User>(API_ENDPOINTS.RESELLER.USERS, userData);
      return result;
    } catch {
      return {
        success: false,
        error: {
          error: 'End User Creation Failed',
          details: 'Failed to create end user',
          timestamp: new Date().toISOString()}
      };
    }
  }
 
  /**
   * Update end user
   */
  async updateEndUser(userId: number, userData: UpdateUserRequest): Promise<ApiResult<User>> {
    try {
      const result = await apiService.put<User>(`${API_ENDPOINTS.RESELLER.USERS}/${userId}`, userData);
      return result;
    } catch {
      return {
        success: false,
        error: {
          error: 'End User Update Failed',
          details: 'Failed to update end user',
          timestamp: new Date().toISOString()}
      };
    }
  }
 
  /**
   * Set end user active status
   */
  async setEndUserActiveStatus(userId: number, active: boolean): Promise<ApiResult<User>> {
    try {
      const result = await apiService.put<User>(
        `/api/reseller/users/${userId}/status`,
        { active }
      );
      return result;
    } catch {
      return {
        success: false,
        error: {
          error: 'User Status Update Failed',
          details: 'Failed to update user status',
          timestamp: new Date().toISOString()}
      };
    }
  }
 
  /**
   * Get devices for specific end user
   */
  async getUserDevices(userId: number): Promise<ApiResult<UserDevicesResponse>> {
    try {
      const result = await apiService.get<UserDevicesResponse>(
        API_ENDPOINTS.RESELLER.USER_DEVICES(userId)
      );
      return result;
    } catch {
      return {
        success: false,
        error: {
          error: 'User Devices Fetch Failed',
          details: 'Failed to fetch user devices',
          timestamp: new Date().toISOString()}
      };
    }
  }
 
  /**
   * Create demo user
   */
  async createDemoUser(demoData: CreateDemoRequest): Promise<ApiResult<User>> {
    try {
      const result = await apiService.post<User>('/api/reseller/demos', demoData);
      return result;
    } catch {
      return {
        success: false,
        error: {
          error: 'Demo User Creation Failed',
          details: 'Failed to create demo user',
          timestamp: new Date().toISOString()}
      };
    }
  }
 
  /**
   * Get demo statistics
   */
  async getDemoStats(): Promise<ApiResult<DemoStats>> {
    try {
      const result = await apiService.get<DemoStats>('/api/reseller/demos/stats');
      return result;
    } catch {
      return {
        success: false,
        error: {
          error: 'Demo Stats Fetch Failed',
          details: 'Failed to fetch demo statistics',
          timestamp: new Date().toISOString()}
      };
    }
  }
 
  /**
   * Get credit usage statistics
   */
  async getCreditUsageStats(): Promise<ApiResult<any>> {
    try {
      const result = await apiService.get<any>('/api/reseller/credits/usage');
      return result;
    } catch {
      return {
        success: false,
        error: {
          error: 'Credit Usage Stats Failed',
          details: 'Failed to fetch credit usage statistics',
          timestamp: new Date().toISOString()}
      };
    }
  }
 
  /**
   * Get reseller purchase pricing config (includes base price + discount tiers)
   * Uses GET /api/reseller/credits/rates which now returns legacy per-user rates plus pricing
   */
  async getCreditPurchaseRates(): Promise<ApiResult<any>> {
    try {
      const result = await apiService.get<any>(API_ENDPOINTS.RESELLER.CREDIT_RATES);
      return result;
    } catch {
      return {
        success: false,
        error: {
          error: 'Purchase Rates Fetch Failed',
          details: 'Failed to fetch credit purchase pricing',
          timestamp: new Date().toISOString()}
      };
    }
  }
 
  /**
   * Create Stripe Checkout Session and return redirect URL
   */
  async createCheckoutSession(quantity: number): Promise<ApiResult<{ url: string }>> {
    try {
      const result = await apiService.post<{ url: string }>(
        API_ENDPOINTS.RESELLER.CHECKOUT_SESSION,
        { quantity }
      );
      return result;
    } catch {
      return {
        success: false,
        error: {
          error: 'Checkout Session Failed',
          details: 'Failed to create Stripe Checkout session',
          timestamp: new Date().toISOString()}
      };
    }
  }
}
 
export const resellerService = new ResellerService();